package UI;

import dataBase.goodsData;
import dataBase.memberData;
import service.goodsService;
import service.memberService;

import java.util.ArrayList;
import java.util.Scanner;

import static java.lang.System.exit;

public class manager {
    goodsService good = new goodsService();
    memberService member = new memberService();
    public static int PASSWORD = 123456;
    Scanner sc = new Scanner(System.in);

    public manager() {
        while (true) {
            System.out.println("---------超市管理--------");
            System.out.println("|\t 1-商品管理");
            System.out.println("|\t 2-会员管理");
            System.out.println("|\t 3-结账");
            System.out.println("|\t 4-修改管理员密码");
            System.out.println("|\t 5-退出系统");
            System.out.println("------------------------");
            System.out.println("请输入数字选择：");
            int inputNo;
            Scanner in = new Scanner(System.in);
            inputNo = in.nextInt();
            switch (inputNo) {
                case 1:
                    System.out.println("执行商品管理的功能");
                    good.goodsServiceUI();
                    break;
                case 2:
                    System.out.println("执行会员管理的功能");
                    member.memberServiceUI();
                    break;
                case 3:
                    System.out.println("执行结账的功能");
                    checkoutUI();
                    break;
                case 4:
                    System.out.println("输入新密码");
                    PASSWORD = sc.nextInt();
                    System.out.println("修改成功");
                    System.out.println("输入管理员密码");
                    int inpassword = sc.nextInt();
                    if (manager.PASSWORD == inpassword) {
                        System.out.println("密码正确");
                    } else {
                        System.out.println("刚改完就忘了？？？？？");
                        System.out.println("执行退出的功能");
                        exit(0);
                    }
                    break;
                case 5:
                    System.out.println("执行退出的功能");
                    exit(0);
                    break;
                default:
                    System.out.println("请输入数字1-4");
            }
        }
    }

    public void checkoutUI() {
        Scanner sc = new Scanner(System.in);
        System.out.println("|----------今日打折菜单------------|");
        good.dao.checkDiscount();
        System.out.println("|-------------------------------|\n");
        System.out.println("请输入编号添加购买商品,输入-1结束");
        float allPrice = 0.0f;
        while (true) {
            ArrayList<goodsData> checkoutList = new ArrayList<>();
            while (true) {
                good.dao.listGoodsData();
                int chooseGoods = sc.nextInt();
                if (chooseGoods != -1) {
                    System.out.println("请输入购买数量");
                    float num = sc.nextInt();
                    for (goodsData goods : good.dao.goodsList) {
                        if (goods.getGoodsId() == chooseGoods) {
                            goodsData good = new goodsData();
                            good.setNumber(num);
                            good.setGoodsId(goods.getGoodsId());
                            good.setName(goods.getName());
                            good.setPrice(goods.getPrice());
                            checkoutList.add(good);
                            allPrice += (good.getPrice() * good.getNumber());
                        }
                    }
                    System.out.println("继续添加或输入-1结束");
                } else {
                    break;
                }
            }
            System.out.println("----------购物小票----------");
            System.out.println("商品名\t单价\t总价");
            for (goodsData goods : checkoutList) {
                System.out.println(STR."\{goods.getName()}\t\{goods.getPrice()}\t\{goods.getPrice() * goods.getNumber()}");
            }
            System.out.println("---------------------------");
            System.out.println(STR."合计:\{allPrice}");
            //结账单打印
            System.out.println("请选择付款方式：1—现金，2-会员卡");
            int cheakoutFrom = sc.nextInt();
            if (cheakoutFrom == 1) {
                System.out.print("请输入付款金额:");
                float jine = sc.nextFloat();
                System.out.println(STR."用户付款:\{jine}");
                System.out.println(STR."找零:\{jine - allPrice}");
                System.out.println("---------------------------");
                System.out.println("\n");
            } else if (cheakoutFrom == 2) {
                System.out.println("请输入会员卡号");
                int memberNumber = sc.nextInt();
                if (member.dao.haveMember(memberNumber)) {
                    System.out.println("账号不存在，已取消支付");
                    System.out.println("\n");
                } else {
                    for (memberData members : member.dao.memberList) {
                        if (members.getMemberId() == memberNumber) {
                            if (members.getMemberMoney() - allPrice >= 0) {
                                members.setMemberMoney(members.getMemberMoney() - allPrice);
                                System.out.println(STR."会员消费后余额:\{members.getMemberMoney()}");
                                System.out.println("\n");
                            } else {
                                System.out.println("余额不足，请充值");
                                System.out.println("输入充值金额");
                                float memberMoney = sc.nextFloat();
                                member.dao.charge(memberNumber, memberMoney);
                                members.setMemberMoney(members.getMemberMoney() - allPrice);
                                System.out.println(STR."会员消费后余额:\{members.getMemberMoney()}");
                                System.out.println("\n");
                                System.out.println("---------------------------");
                            }
                        }
                    }
                }
            }
            System.out.println("1，继续结账 2，返回主页");
            int choose = sc.nextInt();
            switch (choose) {
                case 1:
                    checkoutUI();
                    break;
                case 2:
                    new manager();
                    break;
            }
        }
    }
}
